package br.gerenciadorAudio;

import java.text.ParseException;
import java.util.Calendar;

import br.gerenciadorAudio.BLL.Common;
import br.gerenciadorAudio.BLL.RegraNegocio;
import br.gerenciadorAudio.Model.Profile;
import br.gerenciadorAudio.Model.ProfileSchedule;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TimePicker;
import android.widget.Toast;

public class Schedule extends Activity {

	private int idPerfil;
	
	private CheckBox chkStatus;
	private CheckBox chkSegunda;
	private CheckBox chkTerca;
	private CheckBox chkQuarta;
	private CheckBox chkQuinta;
	private CheckBox chkSexta;
	private CheckBox chkSabado;
	private CheckBox chkDomingo;
	private TimePicker timeInicio;
	private TimePicker timeFim;
	private int idPerfilAgendamento;
	private RegraNegocio rn;

	@Override
	 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(br.gerenciadorAudio.R.layout.schedule);
      
      Intent i = getIntent();
      Bundle b = i.getExtras();
      idPerfil = Integer.parseInt(b.getString(Profile.Colunas.Id));
      rn = new RegraNegocio(getApplicationContext());
      ProfileSchedule profileSchedule;
      LoadControls();
      try 
      {
    	profileSchedule = rn.FindProfileScheduleByProfile(idPerfil);
		
		if(profileSchedule.Id != 0)
		{
	    	  idPerfilAgendamento = profileSchedule.Id;
	    	  LoadData(profileSchedule);
		}
		else
			chkStatus.setChecked(true);
		
      } catch (Exception e) {
    	  // TODO Auto-generated catch block
    	  e.printStackTrace();
      }
      
      
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 0, 0, "Salvar");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			try {
				SalvarDados();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		return true;
	}


	private void SalvarDados() throws ParseException {
		// TODO Auto-generated method stub
		LoadControls();
		
		ProfileSchedule profileSchedule = new ProfileSchedule();
		profileSchedule.IdPerfil = idPerfil;
		profileSchedule.Id = idPerfilAgendamento;
		profileSchedule.Monday = chkSegunda.isChecked();
		profileSchedule.Tuesday = chkTerca.isChecked();
		profileSchedule.Wednesday = chkQuarta.isChecked();
		profileSchedule.Thursday = chkQuinta.isChecked();
		profileSchedule.Friday = chkSexta.isChecked();
		profileSchedule.Saturday = chkSabado.isChecked();
		profileSchedule.Sunday = chkDomingo.isChecked();
		profileSchedule.DateOfBegin = Common.ConvertToTimeString(timeInicio.getCurrentHour(), timeInicio.getCurrentMinute());
		profileSchedule.DateOfEnd = Common.ConvertToTimeString(timeFim.getCurrentHour(), timeFim.getCurrentMinute());
		profileSchedule.Active = chkStatus.isChecked();
		
		RegraNegocio rn = new RegraNegocio(getApplicationContext());
		
		long row;
		if(idPerfilAgendamento == 0)
			row = rn.Insert(profileSchedule);
		else
			row = rn.Update(profileSchedule);
			
		if(row > 0)
			Toast.makeText(getApplicationContext(), "Agendamento Salvo.", Toast.LENGTH_SHORT).show();
		else
			Toast.makeText(getApplicationContext(), "Ocorreu um erro.", Toast.LENGTH_SHORT).show();
	}
	
	private void LoadData(ProfileSchedule profileSchedule) throws ParseException
	{
		LoadControls();
		
		chkDomingo.setChecked(profileSchedule.Sunday);
		chkSabado.setChecked(profileSchedule.Saturday);
		chkSegunda.setChecked(profileSchedule.Monday);
		chkTerca.setChecked(profileSchedule.Tuesday);
		chkQuarta.setChecked(profileSchedule.Thursday);
		chkQuinta.setChecked(profileSchedule.Wednesday);
		chkSexta.setChecked(profileSchedule.Friday);
		chkStatus.setChecked(profileSchedule.Active);
		timeInicio.setCurrentHour(Common.GetHour(profileSchedule.DateOfBegin));
		timeInicio.setCurrentMinute(Common.GetMinute(profileSchedule.DateOfBegin));
		
		timeFim.setCurrentHour(Common.GetHour(profileSchedule.DateOfEnd));
		timeFim.setCurrentMinute(Common.GetMinute(profileSchedule.DateOfEnd));
	}
	
	private void LoadControls()
	{
		  chkSegunda = (CheckBox)findViewById(R.id.chkSegunda);
	      chkTerca = (CheckBox)findViewById(R.id.chkTerca);
	      chkQuarta = (CheckBox)findViewById(R.id.chkQuarta);
	      chkQuinta = (CheckBox)findViewById(R.id.chkQuinta);
	      chkSexta = (CheckBox)findViewById(R.id.chkSexta);
	      chkSabado = (CheckBox)findViewById(R.id.chkSabado);
	      chkDomingo = (CheckBox)findViewById(R.id.chkDomingo);
	      timeInicio = (TimePicker)findViewById(R.id.timeInicio);
	      timeFim = (TimePicker)findViewById(R.id.timeFim);
		  chkStatus = (CheckBox)findViewById(R.id.chkStatus);
	}

}
